﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using BankCreditPortfolio.DAL.Entities;
using BankCreditPortfolio.DAL.Services;

namespace BankCreditPortfolio.Core.Helpers
{
    public class OrderHistoryItem
    {
        public string BankerName { get; set; }
        public string BamkerRole { get; set; }
        public string Status { get; set; }
        public string Comment { get; set; }
        public DateTime? ActionDate { get; set; }
    }

    public static class OrderHistoryHelper
    {
        public static IEnumerable<OrderHistoryItem> GetOrderHistory(bool inProgressOrder, string currentBankerMail, int orderId)
        {
            var result = new List<OrderHistoryItem>();
            var orderHistory = GetOrderHistory(orderId);
            if (orderHistory.OrderInfo == null)
            {
                //there is no order info in DB
                return result;
            }

            if (!inProgressOrder)
            {
                FillForArchive(orderHistory, result);
                        if (result.Count > 0)
                            result.Last().Status = orderHistory.OrderInfo.OrderStatus.Status;
                return result;
            }

            var role = (new BankerService()).GetByEmail(currentBankerMail).Account.Role.Title;
            switch (role)
            {
                case "OperationsAnalyst":
                    break;
                case "SafetyOfficer":
                   result.Add(GetOperationsAnalystHistoryItem(orderHistory));
                    break;
                case "CustomerEngineer":
                    result.Add(GetOperationsAnalystHistoryItem(orderHistory));
                    result.Add(GetSafetyOfficerHistoryItem(orderHistory));
                    break;
                case "CreditCommitteeMember":
                    result.Add(GetOperationsAnalystHistoryItem(orderHistory));
                    result.Add(GetSafetyOfficerHistoryItem(orderHistory));
                    result.Add(GetCustomerEngineerHistoryItem(orderHistory));
                    break;
            }
            return result;
        }

        private static void FillForArchive(OrderHistory orderHistory, List<OrderHistoryItem> history)
        {
            history.Clear();

            if (orderHistory.OperationsAnalyst == null)
                return;
            history.Add(GetOperationsAnalystHistoryItem(orderHistory));
            if (orderHistory.SafetyOfficer == null)
                return;
            history.Add(GetSafetyOfficerHistoryItem(orderHistory));
            if (orderHistory.CustomerEngineer == null)
                return;
            history.Add(GetCustomerEngineerHistoryItem(orderHistory));
            if (orderHistory.CreditCommittieMember == null)
                return;
            history.Add(GetCreditCommittieMemberHistoryItem(orderHistory));
        }

        public static OrderHistory GetOrderHistory(int orderId)
        {
            var orderHistories = (new OrderHistoryService()).GetAll().Where(x => x.OrderInfo.OrderId == orderId);

            OrderHistory orderHistory = orderHistories.Count() == 0
                ? new OrderHistory()
                : orderHistories.First();

            return orderHistory;
        }

        private static OrderHistoryItem GetOperationsAnalystHistoryItem(OrderHistory orderHistory)
        {
            return new OrderHistoryItem
            {
                BamkerRole = "Operations Analyst",
                BankerName = orderHistory.OperationsAnalyst != null
                                 ? AccountHelper.GetUsername(
                                     orderHistory.OperationsAnalyst.Account.Email)
                                 : "Unknown",
                Comment = orderHistory.CommentsOperationsAnalyst,
                Status = "Approved",
                ActionDate = orderHistory.DateOperationsAnalyst
            };
        }

        private static OrderHistoryItem GetSafetyOfficerHistoryItem(OrderHistory orderHistory)
        {
            return new OrderHistoryItem
            {
                BamkerRole = "Safety Officer",
                BankerName = orderHistory.SafetyOfficer != null
                                 ? AccountHelper.GetUsername(
                                     orderHistory.SafetyOfficer.Account.Email)
                                 : "Unknown",
                Comment = orderHistory.CommentsSafetyOfficer,
                Status = "Approved",
                ActionDate = orderHistory.DateSafetyOfficer
            };
        }

        private static OrderHistoryItem GetCustomerEngineerHistoryItem(OrderHistory orderHistory)
        {
            return new OrderHistoryItem
            {
                BamkerRole = "Customer Engineer",
                BankerName = orderHistory.CustomerEngineer != null
                                 ? AccountHelper.GetUsername(
                                     orderHistory.CustomerEngineer.Account.Email)
                                 : "Unknown",
                Comment = orderHistory.CommentsCustomerEngineer,
                Status = "Approved",
                ActionDate = orderHistory.DateCustomerEngineer
            };
        }

        private static OrderHistoryItem GetCreditCommittieMemberHistoryItem(OrderHistory orderHistory)
        {
            return new OrderHistoryItem
            {
                BamkerRole = "Credit Committee Member",
                BankerName = orderHistory.CreditCommittieMember != null
                                 ? AccountHelper.GetUsername(
                                     orderHistory.CreditCommittieMember.Account.Email)
                                 : "Unknown",
                Comment = orderHistory.CommentsCreditCommittieMember,
                Status = "Approved",
                ActionDate = orderHistory.DateCreditCommittieMember
            };
        }
    }
}
